package controller.common;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojos.common.ReplyMessage;
import pojos.parents.ParentsInfo;
import pojos.student.StudentInfo;
import pojos.teacher.TeacherInfo;
import service.CommonLoginService;
import service.ParentsInfoService;
import service.StudentInfoService;
import service.TeacherInfoService;
import utils.InputVerifyUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录处理
 */
@Controller
public class CommonLoginController {

    @Resource(name = "commonLoginService")
    private CommonLoginService loginService;

    @Resource(name = "studentInfoService")
    private StudentInfoService studentInfoService;

    @Resource(name = "teacherInfoService")
    private TeacherInfoService teacherInfoService;

    @Resource(name = "parentsInfoService")
    private ParentsInfoService parentsInfoService;

    /**
     * 管理员登录信息提交
     */
    @PostMapping("/login/admin-login-submit")
    @ResponseBody
    public ReplyMessage adminLoginSubmit(String loginName, String password, String imgAuthCode, HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();

        loginName = loginName.trim();
        if(InputVerifyUtils.isLoginNameNotRight(loginName)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("用户名不正确");
            return replyMessage;
        }

        if(InputVerifyUtils.isPasswordNotRight(password)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("密码不正确");
            return replyMessage;
        }

        HttpSession session = request.getSession();
        if(InputVerifyUtils.isImgAuthCodeNotRight(session,imgAuthCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("图片验证码不正确");
            return replyMessage;
        }

        // 清除该Session中的值
        session.removeAttribute("ImgAuthCode");

        replyMessage = loginService.adminLogin(loginName,password,request);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("AdminInfo",replyMessage.getObject());
        }

        return replyMessage;
    }

    /**
     * 用户登录信息提交
     */
    @PostMapping("/login-submit")
    @ResponseBody
    public ReplyMessage userLoginSubmit(String userName, String password, String authCode, String identity, String freeLogin, HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();

        // 验证码验证
        if(InputVerifyUtils.isImgAuthCodeNotRight(session,authCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("验证码不正确！");
            return replyMessage;
        }
        session.removeAttribute("ImgAuthCode");

        // 登录名验证
        userName = userName.trim();
        if(InputVerifyUtils.isLoginNameNotRight(userName)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名不正确！");
            return replyMessage;
        }

        // 密码验证
        if(InputVerifyUtils.isPasswordNotRight(password)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("密码不正确！");
            return replyMessage;
        }

        // 登录身份验证
        switch (identity){
            case "S": {
                replyMessage = studentInfoService.studentLoginCheck(userName, password);
                session.setAttribute("Role","S");
                break;
            }
            case "T": {
                replyMessage = teacherInfoService.teacherLoginCheck(userName,password);
                session.setAttribute("Role","T");
                break;
            }
            case "P": {
                replyMessage = parentsInfoService.parentsLoginCheck(userName, password);
                session.setAttribute("Role","P");
                break;
            }
            default:{
                replyMessage.setStatus(400);
                replyMessage.setInfo("登录身份不能识别，请重新刷新界面后尝试登录");
                return replyMessage;
            }
        }

        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",replyMessage.getObject());
        }

        // 是否保存密码
        if(freeLogin.equals("Y")){
            session.setAttribute("RememberPswd","YES");
        }else{
            session.setAttribute("RememberPswd","NO");
        }

        return replyMessage;
    }

    /**
     * 查找是否存在该用户名对应的手机号
     */
    @PostMapping("/phone-check")
    @ResponseBody
    public ReplyMessage queryPhoneByLoginName(String loginName,String phoneNum,String imgCode,
                                              Integer identity,HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();

        // 登录名称验证
        if(InputVerifyUtils.isLoginNameNotRight(loginName)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名称不正确！");
            return replyMessage;
        }

        // 手机号验证
        if(InputVerifyUtils.isPhoneNotRight(phoneNum)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("手机号码不正确！");
            return replyMessage;
        }

        // 图片验证码验证
        if(InputVerifyUtils.isImgAuthCodeNotRight(session,imgCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("图片验证码不正确！");
            return replyMessage;
        }

        // 身份信息
        if(identity == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("身份信息不正确！");
            return replyMessage;
        }

        switch (identity){
            case 1: {
                StudentInfo studentInfo = studentInfoService.queryPhoneByName(loginName,phoneNum);
                if(studentInfo != null){
                    session.setAttribute("ForgetPassword",studentInfo);
                    replyMessage.setStatus(200);
                    return replyMessage;
                }else{
                    replyMessage.setStatus(400);
                    replyMessage.setInfo("登录名称或手机号错误，请检查您的登录身份、登录名和手机号码");
                    return replyMessage;
                }
            }
            case 2: {
                TeacherInfo teacherInfo = teacherInfoService.queryPhoneByName(loginName,phoneNum);
                if(teacherInfo != null){
                    session.setAttribute("ForgetPassword",teacherInfo);
                    replyMessage.setStatus(200);
                    return replyMessage;
                }else {
                    replyMessage.setStatus(400);
                    replyMessage.setInfo("登录名称或手机号错误，请检查您的登录身份、登录名和手机号码");
                    return replyMessage;
                }
            }
            case 3: {
                ParentsInfo parentsInfo = parentsInfoService.queryPhoneByName(loginName,phoneNum);
                if(parentsInfo != null){
                    session.setAttribute("ForgetPassword",parentsInfo);
                    replyMessage.setStatus(200);
                    return replyMessage;
                }else{
                    replyMessage.setStatus(400);
                    replyMessage.setInfo("登录名称或手机号错误，请检查您的登录身份、登录名和手机号码");
                    return replyMessage;
                }
            }
            default:{
                replyMessage.setStatus(400);
                replyMessage.setInfo("登录身份信息不正确，请选择正确的登录身份");
                return replyMessage;
            }
        }
    }

    /**
     * 忘记密码提交
     */
    @PostMapping("/forgetPassword-submit")
    @ResponseBody
    public ReplyMessage forgetPasswordCheck(String imgCode,String smsCode,HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();

        // 图片验证码验证
        if(InputVerifyUtils.isImgAuthCodeNotRight(session,imgCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("图片验证码不正确！");
            return replyMessage;
        }

        // 短信验证码验证
        if(InputVerifyUtils.isSmsCodeNotRight(session,smsCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("短信验证码不正确！");
            return replyMessage;
        }

        Object passwordObj = session.getAttribute("ForgetPassword");
        if(passwordObj == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("您的验证信息已经过期，请重新刷新页面后重新提交数据");
            return replyMessage;
        }

        // 清空Session值
        session.removeAttribute("ImgAuthCode");
        session.removeAttribute("SmsAuthCode");

        replyMessage.setStatus(200);
        return replyMessage;
    }

    /**
     * 忘记密码时修改密码
     */
    @PostMapping("/forgetPassword-updatePassword")
    @ResponseBody
    public ReplyMessage forgetToUpdatePassword(String password,HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();
        Object forgetObj = session.getAttribute("ForgetPassword");

        if(forgetObj == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("身份验证已过期，请重新获取验证码后提交");
            return replyMessage;
        }

        if(InputVerifyUtils.isPasswordNotRight(password)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("密码不正确，请重新输入");
            return replyMessage;
        }

        String identify = "";
        int id;
        if(forgetObj instanceof StudentInfo){
            StudentInfo studentInfo = (StudentInfo) forgetObj;
            id = studentInfo.getId();
            identify = "S";
        }else if(forgetObj instanceof TeacherInfo){
            TeacherInfo teacherInfo = (TeacherInfo) forgetObj;
            id = teacherInfo.getId();
            identify = "T";
        }else{
            ParentsInfo parentsInfo = (ParentsInfo) forgetObj;
            id = parentsInfo.getId();
            identify = "P";
        }

        return loginService.updateUserPassword(id,identify,password);
    }
}
